package com.bwie.system.service.impl;

import com.bwie.common.domain.SysFeature;
import com.bwie.common.domain.SysOpeningHours;
import com.bwie.common.domain.SysShop;
import com.bwie.common.domain.request.SysShopRequest;
import com.bwie.common.domain.vo.SysShopVo;
import com.bwie.common.result.Result;
import com.bwie.system.mapper.SysShopMapper;
import com.bwie.system.service.SysShopService;
import com.bwie.system.utils.FastUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: SysShopServiceImpl
 * @Description:
 * @Author: 平安
 * @Date: 2024/4/25 10:38:49
 */
@Service
public class SysShopServiceImpl implements SysShopService {

    @Autowired
    private SysShopMapper sysShopMapper;


    @Autowired
    private FastUtil fastUtil;

    @Override
    public List<SysShop> shopList(SysShopRequest sysShopRequest) {
        return sysShopMapper.shopList(sysShopRequest);
    }

    @Override
    public List<SysFeature> featureList(Integer shopId) {
        return sysShopMapper.featureList(shopId);
    }

    @Override
    public List<SysOpeningHours> openingHours(Integer shopId) {
        return sysShopMapper.openingHours(shopId);
    }

    @Override
    public Result upload(MultipartFile multipartFile) {
        String url = null;
        try {
            url = fastUtil.upload(multipartFile);
            if (url == null || url.equals("")){
                return Result.error("上传图片失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success("http://43.142.85.104:8888/" + url,"上传成功");
    }

    @Transactional
    @Override
    public Result addShop(SysShopVo sysShopVo) {
        SysShop shop =  sysShopMapper.findShopByName(sysShopVo.getName());
        if (shop == null){
            sysShopMapper.addShop(sysShopVo);
            sysShopMapper.addFeature(sysShopVo.getFeatureName(), sysShopVo.getId());
            sysShopMapper.addOpening(sysShopVo.getOpeningHoursName(),sysShopVo.getId());
            return Result.success();
        }
        return Result.error("你所创建的店铺名已被使用，请更换");
    }
}
